package com.example.myapp.APP;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.ADO.ActivityManager;
import com.example.myapp.Model.Money;
import com.example.myapp.Model.MyData;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Shouru extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener{
    private EditText addmn;
    private EditText addtime;
    private Spinner addlectincon;
    private EditText addremarks;
    private Button addmoney_ok;
    private ImageView addmoney_back;
    private Button addmoneyzhichu;
    private Button addmoneyshouru;
    private Button addmoney_ag;
    private Calendar cal;
    private ImageView addmoneymoneyicon;
    private int year, month, day;
    private List<Money> moneyList=new ArrayList<>();
    private MyData myData;
    int image[]=new int[]{R.drawable.gongzi,R.drawable.jiangjin,R.drawable.butie,R.drawable.zhuanzhang,R.drawable.qita};
    int addimage;
    @SuppressLint("WrongViewCast")



    public void onBackPressed() {
        //此处写退向后台的处理
        Intent intent=new Intent(Shouru.this, Zhichu.class);
        startActivity(intent);
        finish();
    }//手机返回键



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.addjizhang);
        addmn=findViewById(R.id.addmn);
        addtime =findViewById(R.id.addtime);
        addlectincon=findViewById(R.id.addlectincon);
        addremarks=findViewById(R.id.addremarks);
        addmoney_ok=findViewById(R.id.addmoney_ok);
        addmoney_back=findViewById(R.id.addmoney_back);
        addmoneyzhichu=findViewById(R.id.zhichu);
        addmoneyshouru=findViewById(R.id.shouru);
        addmoneymoneyicon=findViewById(R.id.addmoneymoneyicon);
        addmoney_ag=findViewById(R.id.addmoney_ag);
        addtime.setOnClickListener(this);
        addlectincon.setOnItemSelectedListener(this);
        myData=(MyData)getApplication();
        getDate();
        addtime.setText(year+"-"+(month+1)+"-"+day);
        addmoney_ag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addmn.setText("");
              addremarks.setText("");
            }
        });
        addmoneyzhichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Shouru.this, Zhichu.class);
                startActivity(intent);
                finish();
            }
        });
        addmoney_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(addmn.getText().toString().equals(""))
                    Toast.makeText(getApplicationContext(),"您输入的金额有误！", Toast.LENGTH_LONG).show();
                else {
                    Money money=new Money();
                    String ps = addmn.getText().toString();
                    Double moneys = Double.valueOf(ps);
                    String time = addtime.getText().toString();
                    String classify = addlectincon.getSelectedItem().toString();
                    String remarks = addremarks.getText().toString();
                    money.setMoney(moneys);
                    money.setImage(addimage);
                    money.setTime(time);
                    money.setClassify(classify);
                    money.setRemarks(remarks);
                    money.setAuthor(BmobUser.getCurrentUser(User.class));
                    money.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e==null){
                                Log.w("aaaa","上传成功");
                                moneyList= myData.getMoneyList();
                                Collections.reverse(moneyList);//倒序
                                moneyList.add(money);
                                Collections.reverse(moneyList);//倒序
                                myData.setMoneyList(moneyList);
                                Toast.makeText(Shouru.this,"保存成功",Toast.LENGTH_LONG).show();
                            } else {
                                Log.w("aaaa","上传失败");
                                Toast.makeText(Shouru.this,"保存失败",Toast.LENGTH_LONG).show();
                            }
                        }
                    });



                }
           }
        });

        addmoney_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Shouru.this, Jizhangben.class);
                startActivity(intent);
                finish();
            }
        });
    }

    //获取当前日期
    private void getDate() {
        cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);       //获取年月日时
        month = cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
        day = cal.get(Calendar.DAY_OF_MONTH);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.addtime:
                DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        addtime.setText(year + "-" + (++month) + "-" + day);
                    }
                };
                DatePickerDialog dialog=new DatePickerDialog(Shouru.this, DatePickerDialog.THEME_HOLO_LIGHT,
                        listener,year,month,day);
                //主题在这里！后边三个参数为显示dialog时默认的日期，月份从0开始，0-11对应1-12个月
                dialog.show();
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {

        switch (i){

            case 0:
                addimage=image[0];
                addmoneymoneyicon.setImageResource(addimage);
                break;
            case 1:
                addimage=image[1];
                addmoneymoneyicon.setImageResource(addimage);
                break;
            case 2:
                addimage=image[2];
                addmoneymoneyicon.setImageResource(addimage);
                break;
            case 3:
                addimage=image[3];
                addmoneymoneyicon.setImageResource(addimage);
                break;
            case 4:
                addimage=image[4];
                addmoneymoneyicon.setImageResource(addimage);
                break;
            case 5:
                addimage=image[5];
                addmoneymoneyicon.setImageResource(addimage);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }










}
